package xyz.xiezc.spider.statistic;

import lombok.Data;
import xyz.xiezc.ioc.annotation.Inject;
import xyz.xiezc.spider.common.Request;
import xyz.xiezc.spider.common.XBean;
import xyz.xiezc.spider.duplicate.BitMapBloomFilter;

import java.util.*;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author wb-xzc291800
 * @date 2019/04/25 17:05
 */
@Data
public class Site {


    @Inject
    BitMapBloomFilter bitMapBloomFilter;
    
    /**
     * 等待下载的队列的大小
     */
    AtomicInteger awaitDownUrlCount = new AtomicInteger(0);
    /**
     * 所有请求都包含的请求头信息, 主要是一些User-Agent 等信息
     */
    Map<String, String> commonHeader = new HashMap<>();
    /**
     * 种子队列的大小, 每次重启的时候,种子url都会重新下载
     */
    List<Request> seedUrls = new ArrayList<>();
    /**
     * 已经下载解析出来完成的一个流程的链接的数量
     */
    AtomicInteger dealXbeanCount = new AtomicInteger(0);
    /**
     * 出现超过 #{download.maxErrorCount} 次数的异常不在下载的bean戴尔数量
     */
    AtomicInteger errXBeanCount = new AtomicInteger(0);
    /**
     * 记录爬取超过五次以上都出现异常的Xbean的信息和最后一次exception的详细哦
     */
    Queue<XBean> errList = new ConcurrentLinkedQueue<>();
    /**
     * 暂时缓存还没有持久化的请求
     */
    Set<String> bloomFilterUrlsCache = new HashSet<>();

    public void addErrXBean(XBean xBean) {
        errList.add(xBean);
        errXBeanCount.addAndGet(1);
    }

    public void addOne(int status) {
        if (status == 0 || status == 1) {
            awaitDownUrlCount.addAndGet(1);
        }

    }

    public void deleteOne(int status) {
        if (status == 0 || status == 1) {
            awaitDownUrlCount.addAndGet(-1);
        }
    }

    public Map<String, Object> getSatus() {
        Map<String, Object> map = new HashMap<>();
        map.put("dealXbeanCount", dealXbeanCount.get());
        map.put("awaitDownUrlCount", awaitDownUrlCount.get());
        map.put("errXBeanCount", errXBeanCount.get());
        map.put("seedUrls", seedUrls);
        map.put("commonHeaders", commonHeader);
        map.put("bloomFilterCount", bitMapBloomFilter.getCount());
        return map;
    }
}
